package com.algorithm.twohundred2threehundred;

/**
 * @author: tzj
 * @create: 2021-06-07
 **/
public class ReverseList_206 {

    public static void main(String[] args) {
        // head = [1,2,3,4,5]
        ListNode node = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(3);
        node.next = node2;
        node2.next = node3;
        System.out.println(node.val);
        System.out.println(node.next.val);
        System.out.println(node.next.next.val);
        /*****************我是一条华丽的分割线******************/
        System.out.println("*****************我是一条华丽的分割线******************");
        ListNode reverseNode = reverseList(node);
        System.out.println(reverseNode.val);
        System.out.println(reverseNode.next.val);
        System.out.println(reverseNode.next.next.val);
    }

    public static ListNode reverseList(ListNode head) {
        ListNode prev = null;
        ListNode curr = head;
        while (curr != null) {
            ListNode nextTemp = curr.next;
            curr.next = prev;
            prev = curr;
            curr = nextTemp;
        }
        return prev;
    }


    public static class ListNode {
        public int val;
        public ListNode next;

        public ListNode() {
        }

        public ListNode(int val) {
            this.val = val;
        }

        public ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
}
